options compress=YES;
libname manasi ''; 

* MERGE SUMMARY STATS BACK INTO MAIN DATASET;
data manasi.kids_ssr_full_nwithsumm;
	merge 

	manasi.sortkids_ssr_full  
		(keep=hun pan toa cs_toa mft start aptype estpd dob dobyy dod 
		mbrlaf cpstat sex race elgrd apldte dt8080 xmitdo dencde dendte 
		clmdte dibdig dibdig2 dibmdr zebind pdscc do reptyp repcus ap_zip 
		stop_rd estpd n_estpd_char n_estpd_num 
		n_estpd_yr n_estpd_mo n_estpd_day n_estpd_age
		n_diag1_none n_diag1_infec n_diag1_neo n_diag1_endo 
		n_diag1_blood n_diag1_mental n_diag1_nerv n_diag1_sense 
		n_diag1_circ n_diag1_resp n_diag1_dig n_diag1_gu
		n_diag1_preg n_diag1_skin n_diag1_musc n_diag1_cong 
		n_diag1_nat n_diag1_ill n_diag1_inj
		n_diag2_none n_diag2_infec n_diag2_neo n_diag2_endo 
		n_diag2_blood n_diag2_mental n_diag2_nerv n_diag2_sense 
		n_diag2_circ n_diag2_resp n_diag2_dig n_diag2_gu
		n_diag2_preg n_diag2_skin n_diag2_musc n_diag2_cong 
		n_diag2_nat n_diag2_ill n_diag2_inj 
		n_cdrunfav1_date n_cdrunfav2_date
		n_termreas1-n_termreas12 n_termdate
		n_term18_N01 n_term18_N02 n_term18_N03 n_term18_N04 n_term18_N05
		n_term18_N06 n_term18_N07 n_term18_N08 n_term18_N09 n_term18_N10
		n_term18_N11 n_term18_N12 n_term18_N13 n_term18_N14 n_term18_N15
		n_term18_N16 n_term18_N17 n_term18_N18 n_term18_N19 n_term18_N20
		n_term18_N22 n_term18_N23 n_term18_N24 n_term18_N25 n_term18_N27
		n_term18_N30 n_term18_N31 n_term18_N32 n_term18_N33 n_term18_N34
		n_term18_N35 n_term18_N36 n_term18_N37 n_term18_N38 n_term18_N39
		n_term18_N40 n_term18_N41 n_term18_N42 n_term18_N43 n_term18_N44
		n_term18_N45 n_term18_N46 n_term18_N47 n_term18_N48 n_term18_N49
		n_term18_N50 n_term18_N51 n_term18_N52 n_term18_N53 n_term18_N54
		n_term18_P01 
		n_term18_S01 n_term18_S04 n_term18_S05 n_term18_S06 n_term18_S07 
		n_term18_S08 n_term18_S09 n_term18_S10 n_term18_S20 n_term18_S21 
		n_term18_S90 n_term18_S91 
		n_term18_H10 n_term18_H20 n_term18_H30 n_term18_H40 n_term18_H50
		n_term18_H60 n_term18_H70 n_term18_H80 n_term18_H90
		n_term18_M01 n_term18_M02)

	manasi.kids_ssr_full_summ 
		(keep=hun pan n_dobmm n_dobdd n_age18_yr n_age18_mo n_age18_day
		n_diag1ever_none n_diag1ever_infec n_diag1ever_neo n_diag1ever_endo 
		n_diag1ever_blood n_diag1ever_mental n_diag1ever_nerv n_diag1ever_sense 
		n_diag1ever_circ n_diag1ever_resp n_diag1ever_dig n_diag1ever_gu
		n_diag1ever_preg n_diag1ever_skin n_diag1ever_musc n_diag1ever_cong 
		n_diag1ever_nat n_diag1ever_ill n_diag1ever_inj
		n_diag2ever_none n_diag2ever_infec n_diag2ever_neo n_diag2ever_endo 
		n_diag2ever_blood n_diag2ever_mental n_diag2ever_nerv n_diag2ever_sense 
		n_diag2ever_circ n_diag2ever_resp n_diag2ever_dig n_diag2ever_gu
		n_diag2ever_preg n_diag2ever_skin n_diag2ever_musc n_diag2ever_cong 
		n_diag2ever_nat n_diag2ever_ill n_diag2ever_inj
		n_diag_perm
		n_term18ever_N01 n_term18ever_N02 n_term18ever_N03 n_term18ever_N04 n_term18ever_N05
		n_term18ever_N06 n_term18ever_N07 n_term18ever_N08 n_term18ever_N09 n_term18ever_N10
		n_term18ever_N11 n_term18ever_N12 n_term18ever_N13 n_term18ever_N14 n_term18ever_N15
		n_term18ever_N16 n_term18ever_N17 n_term18ever_N18 n_term18ever_N19 n_term18ever_N20
		n_term18ever_N22 n_term18ever_N23 n_term18ever_N24 n_term18ever_N25 n_term18ever_N27
		n_term18ever_N30 n_term18ever_N31 n_term18ever_N32 n_term18ever_N33 n_term18ever_N34
		n_term18ever_N35 n_term18ever_N36 n_term18ever_N37 n_term18ever_N38 n_term18ever_N39
		n_term18ever_N40 n_term18ever_N41 n_term18ever_N42 n_term18ever_N43 n_term18ever_N44
		n_term18ever_N45 n_term18ever_N46 n_term18ever_N47 n_term18ever_N48 n_term18ever_N49
		n_term18ever_N50 n_term18ever_N51 n_term18ever_N52 n_term18ever_N53 n_term18ever_N54
		n_term18ever_P01 
		n_term18ever_S01 n_term18ever_S04 n_term18ever_S05 n_term18ever_S06 n_term18ever_S07 
		n_term18ever_S08 n_term18ever_S09 n_term18ever_S10 n_term18ever_S20 n_term18ever_S21 
		n_term18ever_S90 n_term18ever_S91 
		n_term18ever_H10 n_term18ever_H20 n_term18ever_H30 n_term18ever_H40 n_term18ever_H50
		n_term18ever_H60 n_term18ever_H70 n_term18ever_H80 n_term18ever_H90
		n_term18ever_M01 n_term18ever_M02
		n_fedpaylife n_statpaylife n_combpaylife n_paymonlife 
		n_fedpay18 n_statpay18 n_combpay18 n_paymon18
		n_cdrunfav_life n_cdrunfav_18
		n_removed_life n_removed_18
		n_firstage n_firstpay_yr n_lastage n_lastpay_yr
		n_payavg: n_paymon_: n_combpay_: 
		n_cdrunfavyr_: n_N01unfavyr_: n_N04unfavyr_: n_N20unfavyr_: 
		n_N40unfavyr_: n_N43unfavyr_: n_N44unfavyr_: n_S06unfavyr_: n_M02unfavyr_:
		awddte);

	by hun; 
run;

* CREATE VARIABLE FOR ESTPD OF NEXT RECORD;
proc expand data=manasi.kids_ssr_full_nwithsumm 
			out=manasi.kids_ssr_full_nwithsumm
			method=none;
	by hun;
	id n_estpd_num;
	convert n_estpd_num;
	convert n_estpd_num=n_estpd_next  / transformout=(lead 1);
run;

* OUTPUT RECORD IF IT COVERS A GIVEN FISCAL YEAR (e.g., FY2005);
data manasi.kids_ssr_full_nwithsumm ;
	set manasi.kids_ssr_full_nwithsumm ;
		if n_estpd_next=. then n_estpd_next=99999999;
		label n_estpd_num='Date of current rec (n_estpd_num=estpd)';
		label n_estpd_next='Date of next record (n_estpd_next)';
run;
